<template>
	<v-layout type="main">
		<div class="alike-lis-header">
			<div class="lis-header-left">
				<div class="btns-switch">
					<div @click="isCard = true">
						<img src="@/assets/img/List_card.png" v-if="isCard" />
						<img src="@/assets/img/list_nocard.png" v-else />
					</div>
					<div @click="isCard = false">
						<img src="@/assets/img/list_nolist.png" v-if="isCard" />
						<img src="@/assets/img/list_list.png" v-else />
					</div>
				</div>

				<div class="search-box">
					<el-input
						prefix-icon="el-icon-search"
						v-model="Pagedata.name"
						class="search"
						placeholder=""
						@input="searchChange"
					/>
				</div>
			</div>

			<div class="lis-header-right">
				<el-select size="mini" placeholder="请选择" v-model="showThisActive" @change="getData">
					<el-option label="模型" value="1"></el-option>
					<el-option label="算法" value="2"></el-option>
				</el-select>
			</div>
		</div>

		<ul class="alike-lis" v-if="isCard">
			<li v-for="(item, index) in dataList" :key="index">
				<div class="alike-lis-box">
					<div @click="goCode(item)">
						<v-image
							class="alike-lis-pic"
							:src="item.avatar ? item.avatar : require('@/assets/img/drone1.jpg')"
							alt=""
						/>
					</div>
					<div class="alike-lis-word">
						<p class="alike-lis-title">
							<span>
								<span @click="goCode(item)">{{ item.name }}</span>
								<template v-if="typeof item.status === 'string'">
									<el-tag
										type="danger"
										effect="dark"
										size="mini"
										v-if="['terminated', 'failed', 'Failed'].includes(item.status)"
									>
										{{ statusMap[item.status] }}
									</el-tag>
									<el-tag
										type="success"
										effect="dark"
										size="mini"
										v-else-if="['completed', 'Succeeded', 'running', 'Running'].includes(item.status)"
									>
										{{ statusMap[item.status] }}
									</el-tag>
									<el-tag type="warning" effect="dark" size="mini" v-else>
										{{ statusMap[item.status] }}
									</el-tag>
								</template>
							</span>

							<el-popover placement="top" width="80" trigger="click" v-model="item.visible">
								<p>您确定要删除吗？</p>
								<div style="text-align: right; margin: 0">
									<el-button size="mini" type="text" @click="item.visible = false">取消</el-button>
									<el-button type="primary" size="mini" @click="del(item)">确定</el-button>
								</div>
								<i slot="reference" class="el-icon-delete delete-btn" />
							</el-popover>
						</p>

						<p class="alike-lis-hint">{{ item.desc }}</p>

						<div class="alike-lis-user">
							<span class="user-name">创建人：{{ item.create_user.nick_name }}</span>
							<span class="date">{{ item.create_time }}</span>
						</div>
					</div>
				</div>
			</li>
		</ul>

		<div class="table-layout" v-else>
			<el-table :data="dataList" class="myself-table">
				<el-table-column prop="name" label="名称">
					<template slot-scope="{ row }">
						<el-popover placement="right-start" width="320" trigger="hover">
							<row-data :row="row" />
							<el-button
								type="text"
								@click="$router.push({ path: '/ReinforcedDisclosure/code', query: { id: row.id } })"
								slot="reference"
								>{{ row.name }}</el-button
							>
						</el-popover>
					</template>
				</el-table-column>
				<el-table-column prop="desc" label="描述信息">
					<template slot-scope="{ row }">
						<el-popover placement="top" width="320" trigger="hover" :content="row.desc">
							<span class="desc" slot="reference">{{ row.desc }}</span>
						</el-popover>
					</template>
				</el-table-column>

				<el-table-column prop="create_user.nick_name" label="创建人" />
				<el-table-column prop="update_time" label="更新时间" />

				<el-table-column label="操作">
					<template slot-scope="{ row }">
						<!--<el-button type="text" @click="put(row)">修改</el-button>-->
						<!-- <el-button type="text" @click="del(row)">删除</el-button> -->
						<el-popover placement="top" width="80" trigger="click" v-model="row.visible">
							<p>您确定要删除吗？</p>
							<div style="text-align: right; margin: 0">
								<el-button size="mini" type="text" @click="row.visible = false">取消</el-button>
								<el-button type="primary" size="mini" @click="del(row)">确定</el-button>
							</div>
							<el-button type="text" slot="reference">删除</el-button>
						</el-popover>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<div class="pagination-box">
			<el-pagination
				background
				class="pagination"
				layout="total, prev, pager, next, jumper"
				:page-size="Pagedata.pageSize"
				:current-page.sync="Pagedata.pageNum"
				@current-change="handleCurrentChange"
				:total="Pagedata.total"
			>
			</el-pagination>
		</div>
	</v-layout>
</template>

<script>
import { mapActions, mapState } from "vuex";
import { getMarketAlgorithms } from "@/api/marketAlgorithm";
import { delMarketAlgorithm } from "@/api/marketAlgorithm";
export default {
	data() {
		return {
			Pagedata: {
				pageNum: 1,
				pageSize: 12,
				total: 0,
				resourceType: 0, // 数据集
				name: "", // 名称搜索
				order: "recommend", // 排序recommend、createTime、likeCount
			},
			showThisActive: "2",
			dataList: [],
			testPic: require("@/assets/img/test-lis-pic.png"),
			isCard: true,
		};
	},
	mounted() {},
	async created() {
		this.Pagedata = { ...this.Pagedata, pageNum: this.pageNum, name: this.searchValue };
		this.getData();
	},
	destroyed() {},
	computed: mapState({
		pageNum: (state) => state.menuStore.pageData.pageNum,
		searchValue: (state) => state.menuStore.searchValue,
	}),
	methods: {
		...mapActions(["updatePageNum", "updateSearch"]),

		async getData() {
			let res = await getMarketAlgorithms({ type: this.showThisActive, ...this.Pagedata });
			if (res && res.data) {
				let ret = res.data;
				this.dataList = ret.list;
				this.Pagedata.total = ret.total;
			}
		},
		searchChange() {
			this.updatePageNum(1);
			this.Pagedata.pageNum = 1;
			this.updateSearch(this.Pagedata.name);
			this.getData();
		},
		handleCurrentChange(val) {
			this.Pagedata.current = val;
			this.updatePageNum(val);
			this.getData();
		},
		async del(item) {
			// let res = await this.$isDel();
			// if (res) {
			await delMarketAlgorithm(item);
			this.getData();
			// }
		},
		goCode({ id }) {
			let query = {
				id: id,
				active: this.showThisActive,
			};
			this.$router.push({
				path: "/ReinforcedDisclosure/code",
				query,
			});
		},
	},
};
</script>

<style lang="less" scoped>
.add {
	position: absolute;
	right: 28px;
	top: 56px;
}

.alike-lis-title {
	display: flex;
	justify-content: space-between;

	span:first-child {
		width: 400px;
		display: inline-block;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	i {
		cursor: pointer;
	}
}
</style>
